Ext.require([
    'AM.data.OutputNode',
    'AM.data.XMLParser'
]);


Ext.define('AM.service.File', {


    singleton:true,


    saveXML:function () {
        Ext.Msg.prompt('保存PPT', '填写ppt名字', function (btn, text) {
            if (btn == 'ok') {
                var pptName = text;
                var pages = ppt.view.workspace.items.items;
                var tempRoot_node = AM.data.OutputNode.tempNode();
                var ppt_node = AM.data.OutputNode.pptNode(pptName);
                tempRoot_node.appendChild(ppt_node);
                Ext.each(pages, function (page) {
                    var page_node = AM.data.OutputNode.pageNode(page);
                    ppt_node.appendChild(page_node);

                    var cmps = page.items.items;
                    Ext.each(cmps, function (cmp) {
                        var cmp_node = AM.data.OutputNode.cmpNode(cmp);
                        page_node.appendChild(cmp_node)
                    })
                })

                var sendXMLStr = tempRoot_node.innerHTML;

                Ext.Ajax.request({url:'SaveXML', params:{xml:sendXMLStr, pptName:pptName}});
            }
        });
    },


    openXML:function () {
        var url = AM.util.URL.file;
        var xhr = new XMLHttpRequest();
        xhr.open('get', url, true);
        xhr.send(null);
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    var text = xhr.responseText;
                    var items = []
                    var names = text.split('|');
                    Ext.each(names, function (name) {
                        var off;
                        if (-1 != (off = name.indexOf(".xml"))) {
                            name = name.substring(0, off);
                            items.push({
                                xtype:'button',
                                text:name,
                                handler:function(){
                                    ppt.view.thumbnails.removeAll();
                                    ppt.view.workspace.removeAll();
                                    ppt.index = -1;
                                    var win = this.up('window');
                                    win.close();
                                    AM.data.XMLParser.createPPTByName(name)
                                }
                            })
                        }
                    })

                    var win = Ext.create("Ext.window.Window", {
                        title: '选择文件',
                        width: 300,
                        height: 200,
                        items:items
                    }).show();
                }
            }
        }
    }



})